package implicits

import java.io.File
import implicits.Implicits._
import org.joda.time.DateTime
import shapeless._
import shapeless.labelled._
import utils.Utils

/**
 * Created by Administrator on 2019/11/13
 */
trait MyTTool {

  trait ToLinesRec[L <: HList] {
    def apply(l: L,sep:String): List[List[String]]
  }

  trait LowPriorityToLinesRec {
    implicit def hconsToLinesRec1[K <: Symbol, V, T <: HList](implicit
                                                              wit: Witness.Aux[K],
                                                              tmrT: ToLinesRec[T]
                                                             ): ToLinesRec[FieldType[K, V] :: T] =
      (l: FieldType[K, V] :: T,sep:String) => {

        val listTypeCase = TypeCase[List[String]]
        val booleanTypeCase = TypeCase[List[Boolean]]
        val rs = l.head match {
          case listTypeCase(l) => {
            l.mkString(sep)
          }
          case booleanTypeCase(l) => {
            l.mkString(sep)
          }
          case x => x.toString
        }
        tmrT.apply(l.tail,sep) ::: List(List(wit.value.name, rs))
      }
  }

  object ToLinesRec extends LowPriorityToLinesRec {
    implicit val hnilToLinesRec: ToLinesRec[HNil] = (l: HNil,sep:String) => List.empty

    implicit def hconsToMapRec0[K <: Symbol, V, R <: HList, T <: HList](implicit
                                                                        wit: Witness.Aux[K],
                                                                        gen: LabelledGeneric.Aux[V, R],
                                                                        tmrH: ToLinesRec[R],
                                                                        tmrT: ToLinesRec[T]
                                                                       ): ToLinesRec[FieldType[K, V] :: T] =
      (l: FieldType[K, V] :: T,sep:String) => {
        val newLines = tmrH(gen.to(l.head),sep).map { columns =>
          List(s"${columns(0)}", columns(1))
        }
        tmrT(l.tail,sep) ::: newLines
      }
  }

  implicit class MyT[A](val t: A)  {

    def lines[L <: HList](sep:String=":")(implicit
                          gen: LabelledGeneric.Aux[A, L],
                          tmr: Lazy[ToLinesRec[L]]
                         ) = tmr.value.apply(gen.to(t),sep)

  }


}
